package com.woniu.atms.util;



import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;


public final class DataUtils {
    /**
     * MD5加密
     * @param s 加密前文本
     * @return 返回32位md5值
     * @throws NoSuchAlgorithmException 异常
     */
    public static String getMD5(String s) throws NoSuchAlgorithmException {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        return HexString.byteArrayToHexString(md.digest(s.getBytes()));
    }


    /**
     * 生成订单号
     * @param nowTime 输入时间
     * @return 返回订单号
     */
    public static String generateOrderNum(Date nowTime){
        StringBuffer sb = new StringBuffer("G5-");
        sb.append(new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(nowTime));
        sb.append("-");
        try {
            sb.append(getMD5(UUID.randomUUID().toString()).substring(0,10));
        } catch (NoSuchAlgorithmException e) {
            sb.append("FFFFFFFFFF");
        }
        return sb.toString();
    }

    /**
     * LocalDate转换为DAte
     * @param localDate 转换前
     * @return 转换后
     */
    public static Date localDate2Date(LocalDate localDate){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        java.util.Date date = Date.from(instant);
        return date;
    }

    /**
     * 生成随机车票号
     * @return 返回10字节车票号
     */
    public static String generateTicketNum() {
        try {
            return getMD5(UUID.randomUUID().toString()).substring(0,10);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}
